<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>图片上传</title>
  <link rel="stylesheet" type="text/css" href="http://cdn.staticfile.org/webuploader/0.1.5/webuploader.css">
  <script type="text/javascript" src="https://cdn.staticfile.org/jquery/3.5.0/jquery.min.js"></script>
  <script type="text/javascript" src="http://cdn.staticfile.org/webuploader/0.1.5/webuploader.min.js"></script>
</head>

<body>

<div id="uploader-demo">
  <div id="fileList" class="uploader-list"></div>
  <div id="filePicker">选择图片</div>
</div>
<br/>
<a href="/index">返回图片列表页面</a>

<script type="text/javascript">
  var uploader = WebUploader.create({
    auto: true,
    swf: 'http://cdn.staticfile.org/webuploader/0.1.5/Uploader.swf',
    server: '/minio/upload',
    pick: '#filePicker',
    accept: {
      title: 'Images',
      extensions: 'gif,jpg,jpeg,bmp,png',
      mimeTypes: 'image/*'
    }
  });

  uploader.on('fileQueued', function (file) {
    var $li = $(
      '<div id="' + file.id + '" class="file-item thumbnail">' +
      '<img>' +
      '<div class="info">' + file.name + '</div>' +
      '</div>'
      ),
      $img = $li.find('img');

    var $list = $("#fileList");
    $list.append($li);

    uploader.makeThumb(file, function (error, src) {
      if (error) {
        $img.replaceWith('<span>不能预览</span>');
        return;
      }
      $img.attr('src', src);
    }, 100, 100);
  });

  uploader.on('uploadProgress', function (file, percentage) {
    var $li = $('#' + file.id),
      $percent = $li.find('.progress span');

    if (!$percent.length) {
      $percent = $('<p class="progress"><span></span></p>')
        .appendTo($li)
        .find('span');
    }
    $percent.css('width', percentage * 100 + '%');
  });

  uploader.on('uploadSuccess', function (file) {
    $('#' + file.id).addClass('upload-state-done');
  });

  uploader.on('uploadError', function (file) {
    var $li = $('#' + file.id),
      $error = $li.find('div.error');

    if (!$error.length) {
      $error = $('<div class="error"></div>').appendTo($li);
    }
    $error.text('上传失败');
  });

  uploader.on('uploadComplete', function (file) {
    $('#' + file.id).find('.progress').remove();
  });
</script>

</body>
</html>